package com.lagou.server;

import com.lagou.model.TestMessage;
import com.lagou.model.User;
import com.lagou.model.UserMessage;

import java.io.IOException;

/**
 * 编程实现服务器的主功能
 */
public class ServerView {

    /**
     * 使用合成复用原则
     */
    private ServerInitClose sic;
    private ServerDao sd;

    /**
     * 通过构造方法实现成员变量的初始化
     * @param sic
     */
    public ServerView(ServerInitClose sic, ServerDao sd) {
        this.sic = sic;
        this.sd = sd;
    }

    /**
     * 自定义成员方法实现客户端发来消息的接收并处理
     */
    public void serverReceive() throws IOException, ClassNotFoundException {
        while (true) {
            Object obj = sic.getOis().readObject();
            if (obj instanceof UserMessage) {
                UserMessage tum = (UserMessage) obj;
                System.out.println("接收到的消息是：" + tum);
                // 调用方法实现管理员账号和密码信息的校验
                switch (tum.getType()) {
                    case "managerCheck":
                        if (sd.serverManagerCheck(tum.getUser())) {
                            tum.setType("success");
                        } else {
                            tum.setType("fail");
                        }
                        break;
                    case "userCheck":
                        if (sd.serverStudentCheck(tum.getUser())) {
                            tum.setType("success");
                        } else {
                            tum.setType("fail");
                        }
                        break;
                    case "ModifyPassword":
                        if (sd.passwordCheck(tum)) {
                            tum.setType("PasswordModified");
                        } else {
                            tum.setType("fail");
                        }
                        break;
                    case "AddStudent":
                        if (sd.addStudentCheck(tum)) {
                            tum.setType("StudentAdded");
                        } else {
                            tum.setType("fail");
                        }
                        break;
                    case "ModifyStudent":
                        if (sd.modifyStudentCheck(tum)) {
                            tum.setType("StudentModified");
                        } else {
                            tum.setType("fail");
                        }
                        break;
                    case "SearchStudent":
                        if (sd.searchStudentCheck(tum)) {
                            tum.setType("StudentSearched");
                        } else {
                            tum.setType("fail");
                        }
                        break;
                    case "TestStart":
                        if (sd.testStartCheck(tum)) {
                            tum.setType("TestAllowed");
                        } else {
                            tum.setType("EndExam");
                        }
                        break;
                    case "getScore":
                        if (sd.getScoreCheck(tum)) {
                            tum.setType("ScoreSent");
                        } else {
                            tum.setType("fail");
                        }
                        break;
                    default:
                        break;
                }
                // 将校验结果发送给客户端
                sic.getOos().writeObject(tum);
                System.out.println("服务器发送校验结果成功！");
            }
            if (obj instanceof TestMessage){
                TestMessage ttm = (TestMessage) obj;
                System.out.println("接收到的消息是：" + ttm);
                // 调用方法实现管理员账号和密码信息的校验
                switch (ttm.getType()) {
                    case "AddTest":
                        if (sd.addTestCheck(ttm)) {
                            ttm.setType("ScoreSent");
                        } else {
                            ttm.setType("fail");
                        }
                        break;
                    case "DeleteTest":
                        if (sd.deleteTestCheck(ttm)) {
                            ttm.setType("TestDeleted");
                        } else {
                            ttm.setType("fail");
                        }
                        break;
                    case "ModifyTest":
                        if (sd.modifyTestCheck(ttm)) {
                            ttm.setType("TestModified");
                        } else {
                            ttm.setType("fail");
                        }
                        break;
                    case "SearchTest":
                        if (sd.searchTestCheck(ttm)) {
                            ttm.setType("TestSearched");
                        } else {
                            ttm.setType("fail");
                        }
                        break;
                    default:
                        break;
                }
                // 将校验结果发送给客户端
                sic.getOos().writeObject(ttm);
                System.out.println("服务器发送校验结果成功！");
            }
        }
    }
}
